//
// Created by dddgzc on 2025/2/7.
//

#include "../include/Particle.h"

#include "DrawUtils.h"

Particle::Particle(const Vector2 &position, Atlas *atlas, int lifespan)
    :position(position), atlas(atlas), lifespan(lifespan)
{

}

void Particle::setAtlas(Atlas *atlas) {
    this->atlas = atlas;
}

void Particle::setLifespan(int lifespan) {
    this->lifespan = lifespan;
}

void Particle::setPosition(const Vector2 &position) {
    this->position = position;
}

bool Particle::checkValid() const {
    return valid;
}

void Particle::onUpdate(int delta) {
    timer += delta;
    if (timer >= lifespan) {
        timer = 0;
        idxFrame += 1;
        if (idxFrame >= atlas->getSize()) {
            idxFrame = atlas->getSize() - 1;
            valid = false;
        }
    }
}

void Particle::onDraw(const Camera &camera) {
    draw(camera, atlas->getImage(idxFrame), (int)position.x, (int)position.y);
}

